import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/home/home.vue";
import RankingList from "../views/rankinglist/rankingList.vue";
import PraiseList from "../views/rankinglist/praiseList.vue";
import NewList from "../views/rankinglist/newList.vue";
import AbroadList from "../views/rankinglist/abroadList.vue";
import DomesticList from "../views/rankinglist/domesticList.vue";
import Search from "../views/search/search.vue";
import Opinion from "../views/opinion/opinion.vue";

import Gamelibrary from "../views/gamelibrary/gamelibrary.vue";
import CategoryItem from "../views/gamelibrary/gamelibrayComp/categoryItem.vue";
import My from "../views/my/my.vue";
import Collection from "../views/my/myComp/collection.vue";
import Evaluate from "../views/my/myComp/evaluate.vue";
import Message from "../views/my/myComp/message.vue";
import Historys from "../views/my/myComp/historys.vue";
import GameDisplay from "../views/gamedisplay/gameDisplay.vue";
import Introduce from "../views/gamedisplay/gamedisplaycomp/gameLowerHalfComp/introduce.vue";
import Comments from "../views/gamedisplay/gamedisplaycomp/gameLowerHalfComp/comments.vue";

import GoTop from "../components/gotop/goTop.vue";
import Register from "../views/register/register.vue";
import Forget from "../views/forget/forget.vue";
import InsertGame from "../views/administration/insertGame.vue";
import UserList from "../views/administration/userList.vue";
import Useropinion from "../views/administration/useropinion.vue";
import GameList from "../views/administration/gameList.vue";
import ModifyGame from "../views/administration/modifyGame.vue";
import AdminGameCate from "../views/administration/adminGameCate.vue";
import UpateCate from "../views/administration/upateCate.vue";
import InsertCate from "../views/administration/insertCate.vue";
import Admin from "../components/administrations/admin.vue";

import loading from "../components/loading/loading.vue";

import Login2 from "../views/login/login2.vue";
import Cookie from "js-cookie";

Vue.use(VueRouter);

const routes = [{
        path: "/",
        redirect: "home",
    },

    {
        path: "/loading",
        name: "加载",
        component: loading,
    },

    {
        path: "/login2",
        name: "登录",
        component: Login2,
    },

    {
        path: "/admin",
        component: Admin,
        children: [{
                path: "",
                redirect: "userlist",
            },
            {
                path: "userlist",
                name: "用户列表",
                component: UserList,
                meta: {
                    requirMan: true,
                },
            },
            {
                path: "upatecate",
                name: "修改类别",
                component: UpateCate,
                meta: {
                    requirMan: true,
                },
            },

            {
                path: "admingamecate",
                name: "游戏类别",
                component: AdminGameCate,
                meta: {
                    requirMan: true,
                },
            },

            {
                path: "modifygame",
                name: "修改游戏",
                component: ModifyGame,
                meta: {
                    requirMan: true,
                },
            },

            {
                path: "useropinion",
                name: "用户意见",
                component: Useropinion,
                meta: {
                    requirMan: true,
                },
            },

            {
                path: "gamelist",
                name: "游戏详情",
                component: GameList,
                meta: {
                    requirMan: true,
                },
            },

            {
                path: "insertgame",
                name: "添加游戏",
                component: InsertGame,
                meta: {
                    requirMan: true,
                },
            },

            {
                path: "insertcate",
                name: "新增类别",
                component: InsertCate,
                meta: {
                    requirMan: true,
                },
            },
        ],
    },

    {
        path: "/opinion",
        name: "意见反馈",
        component: Opinion,
    },

    {
        path: "/gotop",
        component: GoTop,
    },

    {
        path: "/forget",
        name: "忘记",
        component: Forget,
    },

    {
        path: "/register",
        name: "注册",
        component: Register,
    },
    {
        path: "/home",
        name: "首页",
        component: Home,
    },
    {
        path: "/search",
        name: "搜索结果",
        component: Search,
    },
    {
        path: "/rankinglist",
        name: "热度榜",
        component: RankingList,
    },
    {
        path: "/praiselist",
        name: "好评榜",
        component: PraiseList,
    },
    {
        path: "/newlist",
        name: "新游榜",
        component: NewList,
    },
    {
        path: "/abroadlist",
        name: "国外榜",
        component: AbroadList,
    },
    {
        path: "/domesticlist",
        name: "国内榜",
        component: DomesticList,
    },
    {
        path: "/gamelibrary",
        name: "游戏库",
        component: Gamelibrary,
    },
    {
        path: "/gamelibrary/categoryitem/:id",
        name: "类别",
        component: CategoryItem,
    },
    {
        path: "/mymessage",
        component: My,
        children: [{
                path: "",
                redirect: "collection",
            },
            {
                path: "collection",
                name: "用户收藏",
                component: Collection,
                meta: {
                    requireAuth: true,
                },
            },
            {
                path: "evaluate",
                name: "用户评价",
                component: Evaluate,
                meta: {
                    requireAuth: true,
                },
            },
            {
                path: "message",
                name: "用户信息",
                component: Message,
                meta: {
                    requireAuth: true,
                },
            },
            {
                path: "historys",
                name: "历史记录",
                component: Historys,
                meta: {
                    requireAuth: true,
                },
            },
        ],
    },
    {
        path: "/gamedisplay/:id",
        component: GameDisplay,
        children: [{
                path: "",
                redirect: "introduce",
            },
            {
                path: "introduce",
                name: "详情",
                component: Introduce,
            },
            {
                path: "Comments",
                name: "评论区",
                component: Comments,
            },
        ],
    },
];

const router = new VueRouter({
    mode: "history",
    base: process.env.BASE_URL,
    routes,
});

router.beforeEach((to, from, next) => {
    if (to.meta.requireAuth) {
        console.log(Cookie.get("userid"));
        if (Cookie.get("userid") != undefined && Cookie.get("userid") != null) {
            // 判断本地是否存在token
            next();
        } else {
            // 未登录,跳转到登陆页面
            next({
                path: "/login2",
            });
        }
    } else {
        next();
    }

    if (to.meta.requirMan) {
        if (Cookie.get("uadmin") != null && Cookie.get("uadmin") != undefined) {
            next();
        } else {
            next({
                path: "/login2",
            });
        }
    } else {
        next();
    }
});

router.beforeEach((to, from, next) => {
    document.title = to.name;
    next();
});

export default router;